local yuheng = fk.CreateSkill {
  name = "cf_yuheng",
  tags = { Skill.Lord, },
}

Fk:loadTranslationTable {
  ["cf_yuheng"] = "玉衡",
  [":cf_yuheng"] = "主公技，准备阶段，若你有蓄谋牌，你可以令所有璃月角色依次选择是否弃置你的一张蓄谋牌，若其弃置，则你可以令其摸两张牌或弃置两张牌。",
  ["#cf_yuheng-ask"] = "弃置%src的一张蓄谋牌。",
  ["#cf_yuheng-cancel"] = "取消",
}

yuheng:addEffect(fk.EventPhaseStart, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(yuheng.name) and player.phase == Player.Start and
        #table.filter(player:getCardIds("j"), function(element, index, array)
          return player:getVirtualEquip(element) and player:getVirtualEquip(element).name == "premeditate"
        end) > 0
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room:getAlivePlayers(), function(element, index, array)
      return element.kingdom == "Liyue"
    end)
    if #targets > 0 and room:askToSkillInvoke(player, { skill_name = yuheng.name }) then
      event:setCostData(self, { tos = targets })
      return true
    else
      return false
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local targets = event:getCostData(self).tos or {}
    for _, p in ipairs(targets) do
      local cards = table.filter(player:getCardIds("j"), function(element, index, array)
        return player:getVirtualEquip(element) and player:getVirtualEquip(element).name == "premeditate"
      end)
      if #cards == 0 then break end
      if room:askToSkillInvoke(p, { skill_name = yuheng.name, prompt = "#cf_yuheng-ask:" .. player.id, }) then
        local to_throw_cards = room:askToCards(p, {
          min_num = 1,
          max_num = 1,
          include_equip = false,
          skill_name = yuheng.name,
          pattern = tostring(Exppattern { id = cards }),
          prompt = "#cf_yuheng-ask:" .. player.id,
          cancelable = true,
          expand_pile = cards,
        })
        if #to_throw_cards == 0 then goto continue end
        room:throwCard(to_throw_cards, yuheng.name, player)
        local chioce = room:askToChoice(player, {
          choices = { "draw2", "discard2", "#cf_yuheng-cancel" },
          skill_name = yuheng.name,
          cancelable = true,
        })
        if chioce == "discard2" then
          room:askToDiscard(p, {
            min_num = 2,
            max_num = 2,
            skill_name = yuheng.name,
            cancelable = false,
            include_equip = true,
          })
        end
        if chioce == "draw2" then
          p:drawCards(2, yuheng.name)
        end
      end
    end
    ::continue::
  end
})

return yuheng
